import { Inject, Controller, Get, Query, Post, Body } from '@midwayjs/decorator';
import { Context } from '@midwayjs/koa';
import { UserService } from '../service/user.service';
import { TreeHandlerService } from '../service/tree.service';
import { LoggerHanderService } from '../service/logs.service';
import {FlatLogsHandlerService} from '../service/logsflat.service'
import { QueryLogsParam } from '../interface';
@Controller('/api')
export class APIController {

  @Inject()
  ctx: Context;

  @Inject()
  userService: UserService;

  @Inject()
  treeHandlerService: TreeHandlerService;

  @Inject()
  loggerHanderService: LoggerHanderService;

  @Inject()
  flatLogsHandlerService: FlatLogsHandlerService;

  @Get('/get_user')
  async getUser(@Query('uid') uid) {
    const user = await this.userService.getUser({ uid });
    return { success: true, message: 'OK', data: user };
  }

  @Get('/xlogs_tree')
  async getTreeStruct() {
    try {
      const treeNodes = await this.treeHandlerService.getTreeStruct();
      return { code: 200, message: '请求成功', data: treeNodes };
    } catch (_) {
      return { code: 500, message: '服务器内部错误', data: [] };
    }
  }

  @Post('/xlogs')
  async getLogs(@Body() params: QueryLogsParam) {
    try {
      const logsList = await this.flatLogsHandlerService.getLogs(params);
      return { code: 200, message: '请求成功', data: logsList };
    } catch (_) {
      return { code: 500, message: '服务器内部错误', data: [] };
    }
  }
}
